<?php
$loader->requireOnce('ordo/TicketItem.class.php');
$loader->requireOnce('ordo/Service.class.php');
$loader->requireOnce('ordo/Department.class.php');
$loader->requireOnce('includes/DynamicMenu.class.php');
$loader->requireOnce('includes/PatientQueue/AbsQueue.class.php');
$loader->requireOnce('includes/AjaxRender/TicketRender.class.php');

class C_Queue extends Controller {
	
	var $template_mod;					
	
	function C_Queue($template_mod = "general") {
		parent::Controller();
		if (Celini::getCurrentController() == 'Queue') {
			$menu =& Menu::getInstance();
			$menu->currentSection = 'queue';
		}
		$this->template_mod = $template_mod;				
		$this->view->path = 'queue';
		
	}		
		
	public function actionRenderQueue() {
		$menuObj = new DynamicMenu($this->_me);
		$result = $menuObj->processQueueMenuItem();
		
		if ($result !== false) {
			Celini::redirect($result['controller'], $result['action']);
		}
	}
	
	public function actionReceptionistQueue() {
		$login2Dept 	= 	$this->_me->get_department_id();
		$personId		=	$this->_me->get_person_id();
		$encouterRoom 	= 	Celini::newORDO('QueueRoom');
		$res 			= 	$encouterRoom->getQueueRooms($login2Dept, $personId);
		if(count($res) > 0){
			$this->assign('encounterRoom', $res);
		}
		
		$this->assignCommonVariables();
		
		return $this->view->render('encounter.html');
	}
	
	public function actionRenderEncounterQueueAjax() {		
		$roomId 		= 	$_GET['room_id'];
		$login2Dept 	= 	$this->_me->get_department_id();
		$personId		=	$this->_me->get_person_id();		
		if ($roomId == 'all') {			
			$roomId = Celini::newORDO('QueueRoom')->getQueueRoomIds($login2Dept, $personId);
		}
		
		$result = $this->renderQueues(array('in_line', 'in_progress', 'in_waiting'), $roomId, $login2Dept);
		return json_encode($result);
	}
	
	public function actionDoctorQueue() {					
		$login2Dept 	= 	$this->_me->get_department_id();
		$personId		=	$this->_me->get_person_id();
		$encouterRoom = Celini::newORDO('QueueRoom');
		$res = $encouterRoom->getQueueRooms($login2Dept, $personId);
		$user = $this->_me->get_user();
		$defaultRoomId = $user->get('room_id');
		$this->assign('encounterRoom', $res);
		$this->assign('defaultRoom', $defaultRoomId);	
		$this->assignCommonVariables();		
		return $this->view->render('doctor.html');					
	}			
	
	public function actionRenderDoctorQueueAjax() {
		$roomId = $_GET['room_id'];
		$login2Dept	=	$this->_me->get_department_id();
		$result = array();				
		$result = $this->renderQueues(array('in_waiting'), $roomId);
		$queue = AbsQueue::getQueue(AbsQueue::ENCOUNTER);
		$queue->setViewType(EncounterQueue::DOCTOR_VIEW);				
		$result['in_line'] = $queue->renderQueueAjax($roomId,$login2Dept);
		
		$queue = AbsQueue::getQueue(AbsQueue::IN_PROGRESS);
		$queue->setViewType(InProgressQueue::DOCTOR_VIEW);
		$result['in_progress'] = $queue->renderQueueAjax($roomId,$login2Dept);
				
		$queue = AbsQueue::getQueue(AbsQueue::WAITING);
		$queue->setViewType(InProgressQueue::DOCTOR_VIEW);
		$result['in_waiting'] = $queue->renderQueueAjax($roomId,$login2Dept);
		
		return json_encode($result);
	}
	
	public function actionChangeDoctor() {	
		$ticketItemId = $_GET['ticketItemId'];	
		$ticketItem = Celini::newORDO('TicketItem', $ticketItemId);
		$status = $ticketItem->get('status');
				
		if ($status == TicketItem::CLOSED) {
			$result = array ('available' => false, 'result' => TicketRender::getTicketItemStatus($status));
			return json_encode($result);
		}
				
		$result = array ('available' => true);		
		
		$doctorList = Celini::newORDO('Employee')->getDoctorList();
		$currentDoctor = $this->_me->get_person_id();
		$text = "<select id='doctorList'>";
		foreach ($doctorList as $id=>$name) {
			if ($id == $currentDoctor) {
				$text .= "<option value='$id' label='$name' selected='selected'>$name</option>";
			} else {
				$text .= "<option value='$id' label='$name'>$name</option>";
			}			
		}
		$text .= "</select>";
		$result['result'] = $text;		
		return json_encode($result);
	}
	
	public function actionSubmitChangeDoctor() {
		$ticketItemId 		= 	$_GET['ticketItemId'];
		$doctorId 			= 	$_GET['doctorId'];				
		$changePrescription	=	$_GET['changePrescription'];
		$queueItem = Celini::newORDO('QueueItem')->fromTicketItemId($ticketItemId);
		$queueItem->set('holder_id', $doctorId);
		$queueItem->persist();
		
		// Change doctor_id on the ticket_item
		$ti = Celini::newORDO('TicketItem', $ticketItemId);
		$ti->set('doctor_id', $doctorId);
		$ti->persist();
		// Change doctor_id on the prescription of the medical record
		if($changePrescription=='true'){
			$this->updateDoctorIdOnPrescription($ticketItemId, $doctorId);
		}
	}
	
	public function actionCashierQueue() {
		$menuObj = new DynamicMenu($this->_me);
		$result = $menuObj->processCashierQueue();
		
		if ($result !== false) {
			Celini::redirect($result['controller'], $result['action']);
		}
	}
	
	public function actionCashierQueueForOutPatient() {
		$this->assignCommonVariables();
		return $this->view->render('cashier.html');
	}
	
	public function actionRenderCashierQueueAjax() {
		$user = $this->_me->get_user();
		$roomId = $user->get('room_id');
			
		$result = $this->renderQueues(array('cashier'), $roomId);		
		$queue = AbsQueue::getQueue(AbsQueue::IN_PROGRESS);
		$queue->setViewType(InProgressQueue::CASHIER_VIEW);
		$result['in_progress'] = $queue->renderQueueAjax($roomId, false);
		return json_encode($result);		
	}
	
	public function actionPreCheckOpeningAjax() {
		$ticketItemId = $_GET['ticket_item_id'];
		$ticketItem = Celini::newORDO('TicketItem', $ticketItemId);
		$status = $ticketItem->get('status');
				
		$queue = AbsQueue::getQueue(AbsQueue::ENCOUNTER);		
		$queueItem = Celini::newORDO('QueueItem');
		$res = $queueItem->isExistant($ticketItemId, $queue->getQueueId());
		
		$result = array ('available' => true, 'status' => TicketRender::getTicketItemStatus($status));
		if ($res === false) {
			$result['available'] = false;					
		}
				
		return json_encode($result);
	}
	

	public function actionPreCheckWaitingAjax() {
		$ticketItemId = $_GET['ticket_item_id'];
		$ticketItem = Celini::newORDO('TicketItem', $ticketItemId);
		$status = $ticketItem->get('status');
				
		$queue = AbsQueue::getQueue(AbsQueue::WAITING);		
		$queueItem = Celini::newORDO('QueueItem');
		$res = $queueItem->isExistant($ticketItemId, $queue->getQueueId());
		
		$result = array ('available' => true, 'status' => TicketRender::getTicketItemStatus($status));
		if ($res === false) {
			$result['available'] = false;					
		}
				
		return json_encode($result);
	}
	
	public function actionPreCheckAvailableAjax() {
		$ticketItemId = $_GET['ticket_item_id'];
		$queue = AbsQueue::getQueue(AbsQueue::CASHIER);
		
		$queueItem = Celini::newORDO('QueueItem');
		$res = $queueItem->isExistant($ticketItemId, $queue->getQueueId());
		
		$result = array ('available' => true, 'status' => "");
		if ($res === false) {
			$result['available'] = false;
			
			$queue = AbsQueue::getQueue(AbsQueue::IN_PROGRESS);
			if ($queueItem->isExistant($ticketItemId, $queue->getQueueId())) {
				 $result['status'] = L10nHelper::_("Collecting money is in progress");
			} else {
				 $result['status'] = L10nHelper::_("Encashed");
			}
		}
		$this->startCashierQueue($ticketItemId);		
		return json_encode($result);
	}
	
	public function assignCommonVariables() {
		$user = $this->_me->get_user();
		$roomId = $user->get('room_id');					
		$personId = $user->get('person_id');
		
		$roomOrdo = Celini::newORDO('Room', $roomId);
		$personOrdo = Celini::newORDO('Person', $personId);
		$this->assign('roomName', $roomOrdo->get('name'));		
		$this->assign('employeeName', $personOrdo->get('name'));	

		$this->assign('today', L10nHelper::renderDateTime(date('Y-m-d')));	
	}
	
	private function startCashierQueue($ticketItemId) {
		$queueItem = Celini::newORDO('QueueItem');
		AbsQueue::moveTicketItemToQueue($ticketItemId, AbsQueue::IN_PROGRESS);					
		
		// add cashier name as holder of this ticket_item
		$user = $this->_me->get_user();		
		$qi = QueueItem::fromTicketItemId($ticketItemId);
		$qi->set('holder_id', $user->get('person_id'));
		$qi->persist();	
	}
	
	private function renderQueues($queues, $roomId, $departmentId=false) {
		$result = array();
		if (in_array('in_line', $queues)) {
			$queue = AbsQueue::getQueue(AbsQueue::ENCOUNTER);
			$result['in_line'] = $queue->renderQueueAjax($roomId,$departmentId);
		}
		
		if (in_array('in_progress', $queues)) {
			$queue = AbsQueue::getQueue(AbsQueue::IN_PROGRESS);
			$result['in_progress'] = $queue->renderQueueAjax($roomId,$departmentId);
		}
		
		if (in_array('in_waiting', $queues)) {
			$queue = AbsQueue::getQueue(AbsQueue::WAITING);
			$result['in_waiting'] = $queue->renderQueueAjax($roomId,$departmentId);
		}
		
		if (in_array('cashier', $queues)) {
			$queue = AbsQueue::getQueue(AbsQueue::CASHIER);
			$result['cashier'] = $queue->renderQueueAjax($roomId,$departmentId);
		}
		return $result;	
	}

	private function updateDoctorIdOnPrescription($ticketItemId, $doctorId) {
		$pres = Celini::newORDO('Prescription');
		$presId = $pres->getPrescriptionIdFromTicketItem($ticketItemId);
		if ( $presId > 0) {
			$pres->updateDoctorId($doctorId, $presId);
		}
	}	
	public function actionRemoveInProgressTicketItem(){
		$ticketItemId	=	$_GET['ticketItemId'];
		$result	=	AbsQueue::moveTicketItemToQueue($ticketItemId,AbsQueue::ENCOUNTER);
		$respone['result']	=	$result;
		return json_encode($respone);
	}
}
?>